package com.zzg.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zzg.bean.Page;
import com.zzg.bean.User;
import com.zzg.service.UserService;
import com.zzg.service.impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/user.do")
public class UserServlet extends BaseServlet{
//	private static UserService service = new UserServiceImpl();
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
	UserService service = context.getBean(UserService.class);

	//查询所有用户
	public void selectAllUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
//		UserService service = new UserServiceImpl();
		List<User> users = service.findAllUser();
		req.setAttribute("users", users);
		req.getRequestDispatcher("/user.jsp").forward(req, resp);
	}
	
	//添加用户
	public void addUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String username = req.getParameter("username");
		String realname = req.getParameter("realname");
		String address = req.getParameter("address");
		String tel = req.getParameter("tel");
		
		username = new String(username.getBytes("ISO-8859-1"),"utf-8");
		realname = new String(realname.getBytes("ISO-8859-1"),"utf-8");
		address = new String(address.getBytes("ISO-8859-1"),"utf-8");
		tel = new String(tel.getBytes("ISO-8859-1"),"utf-8");
		
		User user = new User(username, realname, address, tel);
//		UserService service = new UserServiceImpl();
		
		service.addUser(user);
		selectUserPage(req, resp);
//		resp.sendRedirect("user.do?method=selectAllUser");
	}
	//删除用户
	public void delUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		int id =Integer.parseInt(req.getParameter("id"));
		service.deleteUser(id);
		//直接调用方法，
//		resp.sendRedirect("user.do?method=selectAllUser");
		selectUserPage(req, resp);
	}
	
	//修改用户
	public void updUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
		req.setCharacterEncoding("utf-8");
		int id =Integer.parseInt(req.getParameter("id"));
		String username = req.getParameter("username");
		String realname = req.getParameter("realname");
		String address = req.getParameter("address");
		String tel = req.getParameter("tel");
		
		username = new String(username.getBytes("ISO-8859-1"),"utf-8");
		realname = new String(realname.getBytes("ISO-8859-1"),"utf-8");
		address = new String(address.getBytes("ISO-8859-1"),"utf-8");
		tel = new String(tel.getBytes("ISO-8859-1"),"utf-8");
		
		User user = new User(id,username, realname, address, tel);
		service.updateUser(user);
		selectUserPage(req, resp);
	}
	
	//根据id查询用户
	public void findUserById(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
		int id =Integer.parseInt(req.getParameter("id"));
		User user = service.findUserById(id);
		req.setAttribute("user", user);
		req.getRequestDispatcher("/updUser.jsp").forward(req, resp);
	}
	//根据id查询用户
	public void findUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
			int id =Integer.parseInt(req.getParameter("id"));
			User user = service.findUserById(id);
			resp.setCharacterEncoding("utf-8");
			String json = new Gson().toJson(user); 
			resp.getWriter().print(json);
	}
	//用户名查重（根据用户名查询用户）
	public void checkUsername(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
		//获得参数
		String username = req.getParameter("username");
		username = new String(username.getBytes("ISO-8859-1"),"utf-8");
		User user = service.selectUserbyUsername(username);
//		System.out.println(user);
		resp.getWriter().print(user==null);
	}
	//用户名模糊查询
	public void findOne(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
		String username = req.getParameter("username");
		username = new String(username.getBytes("ISO-8859-1"),"utf-8");
//		List<User> users = service.findUserLikeName(username);
//		System.out.println(users);
		Page<User> page = service.findUserLikeName(username);
		req.setAttribute("page",page);
		req.getRequestDispatcher("/user.jsp").forward(req, resp);
	}
	
	
	//分页
	public void selectUserPage(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
		//获得页数参数
		String pageNo = req.getParameter("pageNo");
		int no = 1;
		if(pageNo != null){
			no = Integer.parseInt(pageNo);
		}
		Page<User> page = service.selectUserPage(no);
		req.setAttribute("page",page);
		req.getRequestDispatcher("/user.jsp").forward(req, resp);
	}
}
